<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>第五章 WXML与WXSS | 我的世界</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/notes/favicon.ico">
    <link rel="manifest" href="/notes/manifest.json">
    <meta name="description" content="学习，生活，还有美食~">
    
    <link rel="preload" href="/notes/assets/css/0.styles.8453c794.css" as="style"><link rel="preload" href="/notes/assets/js/app.f19b5e39.js" as="script"><link rel="preload" href="/notes/assets/js/2.69392339.js" as="script"><link rel="preload" href="/notes/assets/js/55.f724b26c.js" as="script"><link rel="preload" href="/notes/assets/js/14.e376f3f7.js" as="script"><link rel="prefetch" href="/notes/assets/js/10.4748ef2e.js"><link rel="prefetch" href="/notes/assets/js/11.4ec67c3b.js"><link rel="prefetch" href="/notes/assets/js/12.db234a0a.js"><link rel="prefetch" href="/notes/assets/js/13.0bdeeeea.js"><link rel="prefetch" href="/notes/assets/js/15.873f63d0.js"><link rel="prefetch" href="/notes/assets/js/16.6d650151.js"><link rel="prefetch" href="/notes/assets/js/17.f0bcb5c9.js"><link rel="prefetch" href="/notes/assets/js/18.7dffee3a.js"><link rel="prefetch" href="/notes/assets/js/19.5f0252c4.js"><link rel="prefetch" href="/notes/assets/js/20.abea2681.js"><link rel="prefetch" href="/notes/assets/js/21.252713aa.js"><link rel="prefetch" href="/notes/assets/js/22.c99488d0.js"><link rel="prefetch" href="/notes/assets/js/23.3839292f.js"><link rel="prefetch" href="/notes/assets/js/24.9cc2f325.js"><link rel="prefetch" href="/notes/assets/js/25.e9dcd2f1.js"><link rel="prefetch" href="/notes/assets/js/26.31898ae6.js"><link rel="prefetch" href="/notes/assets/js/27.88db2371.js"><link rel="prefetch" href="/notes/assets/js/28.2d29c56c.js"><link rel="prefetch" href="/notes/assets/js/29.a0b42251.js"><link rel="prefetch" href="/notes/assets/js/3.48b2b659.js"><link rel="prefetch" href="/notes/assets/js/30.9b38a2bb.js"><link rel="prefetch" href="/notes/assets/js/31.d374da8e.js"><link rel="prefetch" href="/notes/assets/js/32.7868c3f3.js"><link rel="prefetch" href="/notes/assets/js/33.87730e15.js"><link rel="prefetch" href="/notes/assets/js/34.5a5b6c6e.js"><link rel="prefetch" href="/notes/assets/js/35.1deedbd4.js"><link rel="prefetch" href="/notes/assets/js/36.d39f2b24.js"><link rel="prefetch" href="/notes/assets/js/37.d87637b4.js"><link rel="prefetch" href="/notes/assets/js/38.d118907c.js"><link rel="prefetch" href="/notes/assets/js/39.9f2c8514.js"><link rel="prefetch" href="/notes/assets/js/4.161aee82.js"><link rel="prefetch" href="/notes/assets/js/40.917feb30.js"><link rel="prefetch" href="/notes/assets/js/41.78e1b969.js"><link rel="prefetch" href="/notes/assets/js/42.cd6ac9d4.js"><link rel="prefetch" href="/notes/assets/js/43.961a438d.js"><link rel="prefetch" href="/notes/assets/js/44.ef1661d7.js"><link rel="prefetch" href="/notes/assets/js/45.b89cd059.js"><link rel="prefetch" href="/notes/assets/js/46.8ca9e560.js"><link rel="prefetch" href="/notes/assets/js/47.a35bec74.js"><link rel="prefetch" href="/notes/assets/js/48.953bb15e.js"><link rel="prefetch" href="/notes/assets/js/49.ae614c87.js"><link rel="prefetch" href="/notes/assets/js/5.ef4783db.js"><link rel="prefetch" href="/notes/assets/js/50.a9fbc190.js"><link rel="prefetch" href="/notes/assets/js/51.12ae367b.js"><link rel="prefetch" href="/notes/assets/js/52.57a5efe4.js"><link rel="prefetch" href="/notes/assets/js/53.861eb65b.js"><link rel="prefetch" href="/notes/assets/js/54.3bc1c9a1.js"><link rel="prefetch" href="/notes/assets/js/56.02e48e7a.js"><link rel="prefetch" href="/notes/assets/js/6.72ed1ea6.js"><link rel="prefetch" href="/notes/assets/js/7.f1e39dec.js"><link rel="prefetch" href="/notes/assets/js/8.84b75613.js"><link rel="prefetch" href="/notes/assets/js/9.e1cd5390.js">
    <link rel="stylesheet" href="/notes/assets/css/0.styles.8453c794.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/notes/" class="home-link router-link-active"><img src="/notes/images/fish.gif" alt="我的世界" class="logo"> <span class="site-name can-hide">我的世界</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/notes/notes/" class="nav-link router-link-active">🎓 学习</a></div><div class="nav-item"><a href="/notes/life/" class="nav-link">🏸 生活</a></div> <a href="https://gitee.com/zgj6" target="_blank" rel="noopener noreferrer" class="repo-link">
    git
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/notes/notes/" class="nav-link router-link-active">🎓 学习</a></div><div class="nav-item"><a href="/notes/life/" class="nav-link">🏸 生活</a></div> <a href="https://gitee.com/zgj6" target="_blank" rel="noopener noreferrer" class="repo-link">
    git
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p title="学习笔记" class="sidebar-heading"><span>学习笔记</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="JavaScript 高程笔记" class="sidebar-heading"><span>JavaScript 高程笔记</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="HTTP" class="sidebar-heading"><span>HTTP</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="微信小程序" class="sidebar-heading open"><span>微信小程序</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/notes/notes/wx-use-note/" aria-current="page" title="目录" class="sidebar-link">目录</a></li><li><a href="/notes/notes/wx-use-note/chapter01.html" title="微信小程序" class="sidebar-link">微信小程序</a></li><li><a href="/notes/notes/wx-use-note/chapter02.html" title="遇到的问题" class="sidebar-link">遇到的问题</a></li><li><a href="/notes/notes/wx-use-note/start_01.html" title="第一章 入门准备" class="sidebar-link">第一章 入门准备</a></li><li><a href="/notes/notes/wx-use-note/start_02.html" title="第二章 初步创建小程序" class="sidebar-link">第二章 初步创建小程序</a></li><li><a href="/notes/notes/wx-use-note/start_03.html" title="第三章  文件类别" class="sidebar-link">第三章  文件类别</a></li><li><a href="/notes/notes/wx-use-note/start_04.html" title="第四章 文件部分注意事项" class="sidebar-link">第四章 文件部分注意事项</a></li><li><a href="/notes/notes/wx-use-note/start_05.html" aria-current="page" title="第五章 WXML与WXSS" class="active sidebar-link">第五章 WXML与WXSS</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/wx-use-note/start_05.html#wxml" title="WXML" class="sidebar-link">WXML</a></li><li class="sidebar-sub-header"><a href="/notes/notes/wx-use-note/start_05.html#wxss" title="WXSS" class="sidebar-link">WXSS</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p title="TS" class="sidebar-heading"><span>TS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="Vite" class="sidebar-heading"><span>Vite</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="interviewquestion" class="sidebar-heading"><span>interviewquestion</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="AI" class="sidebar-heading"><span>AI</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="UE" class="sidebar-heading"><span>UE</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="第五章-wxml与wxss"><a href="#第五章-wxml与wxss" class="header-anchor">#</a> 第五章 WXML与WXSS</h1> <h2 id="wxml"><a href="#wxml" class="header-anchor">#</a> WXML</h2> <p>WXML全称WeiXin Markup Language（微信标记语言），与HTML有些不同的是，这是微信框架提供的一套标签语言，也就是说规矩你都的按微信官方规定的走，一些属性或者默认操作和原生js可能会有所差别，或者没有相应的功能，这是一个需要注意的点</p> <p>下面来细说WXML的规则和HTML的不同，以及相同之处</p> <p>先来说说我们经常用的块状元素<kbd>div</kbd>,这个块状元素对应着微信小程序里面 <kbd>view</kbd></p> <p>基本内容组件中还有<kbd>text</kbd>相当于<kbd>span</kbd>，可以用来装载多段行内文本，再如媒体组件中的<kbd>image</kbd>，它相当于<kbd>img</kbd>标签用来承载图片信息。</p> <p>除此之外，每个小程序组件都默认提供了一些非常nice的属性，这里以<kbd>image</kbd>组件为例。</p> <p>假设我们希望图片加载完成后做一件事情，比如获取图片的宽度，这里就可以利用<kbd>image</kbd>的<kbd>bindload</kbd>（图片加载完成后触发）属性，打开我们的小程序开发工具，在index.wxml中添加一个image组件：</p> <div class="language- extra-class"><pre class="language-text"><code>&lt;view&gt;
  &lt;!-- 图片直接拿了哔哩哔哩的，万一以后打不开了，大家自己随便找个图 --&gt;
  &lt;image bindload='imgLoad' src='xxxx'&gt;&lt;/image&gt;
&lt;/view&gt;
</code></pre></div><p>然后页面对应的js里面这样处理</p> <div class="language- extra-class"><pre class="language-text"><code>const app = getApp()

Page({
  //图片加载完成后执行的方法
  imgLoad(image) {
    console.log(image.detail.width);
  }
})
</code></pre></div><p>保存后，就可以看到在编辑器console控制台输出了图片的宽度，这是image组件默认的宽度样式。</p> <p>再例如图片懒加载在页面优化中也是需要考虑的一点，而<kbd>image</kbd>提供了<kbd>lazy-load</kbd>属性用于解决图片懒加载问题。</p> <p>这里就只举例这么多，更多组件的使用和相应的属性官方已经写的很仔细了，直接去<a href="https://developers.weixin.qq.com/miniprogram/dev/component/" target="_blank" rel="noopener noreferrer">官方文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>对着开发就行</p> <h2 id="wxss"><a href="#wxss" class="header-anchor">#</a> WXSS</h2> <p>可以放心的是WXSS并没有对于样式高度拓展，之前CSS样式怎么玩，现在在小程序还是一样的玩。这方面官方还有一定的坑，比如官方说选择器目前只支持如下几种：</p> <table><thead><tr><th>选择器</th> <th>例子</th> <th>含义</th></tr></thead> <tbody><tr><td>#id</td> <td>#parent</td> <td>选择id='parent'的组件</td></tr> <tr><td>.class</td> <td>.child</td> <td>选择所有class='child'的组件</td></tr> <tr><td>element</td> <td>view</td> <td>选择所有view组件</td></tr> <tr><td>element,element</td> <td>view,text</td> <td>选择所有view组件和text组件</td></tr> <tr><td>::after</td> <td>text::after</td> <td>在text组件后面插入内容</td></tr> <tr><td>::before</td> <td>text::before</td> <td>在text组件前面插入内容</td></tr></tbody></table> <p>但事实证明子代选择器，后代选择器以及伪类选择器大部分都支持，比如下面的例子：</p> <div class="language- extra-class"><pre class="language-text"><code>&lt;view class=&quot;box&quot;&gt;
	&lt;text&gt;一往情深深几许，&lt;/text&gt;
	&lt;text&gt;深山夕照深秋雨。&lt;/text&gt;
	&lt;input placeholder=&quot;输入作者名字&quot; /&gt;
&lt;/view&gt;
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>.box&gt;text:first-child {
  color: #e4393c;
}

input:active {
  border: 1px solid #ddd;
}
</code></pre></div><p>但也有不支持的部分，比如在手机上大家是没办法使用:hover伪类，毕竟手机上没有鼠标悬浮。</p> <p>然后关于WXSS，小程序也做了亮点拓展，我们先说新增的尺寸单位rpx，官方描述如下：</p> <blockquote><p>rpx（responsive pixel）: 可以根据屏幕宽度进行自适应。规定屏幕宽为750rpx。如在 iPhone6 上，屏幕宽度为375px，共有750个物理像素，则750rpx = 375px = 750物理像素，1rpx = 0.5px = 1物理像素。</p></blockquote> <p>并提供了如下几个手机型号的转换例子：</p> <table><thead><tr><th>设备</th> <th>rpx换算px (屏幕宽度/750)</th> <th>px换算rpx (750/屏幕宽度)</th></tr></thead> <tbody><tr><td>iPhone5</td> <td>1rpx = 0.42px</td> <td>1px = 2.34rpx</td></tr> <tr><td>iPhone6</td> <td>1rpx = 0.5px</td> <td>1px = 2rpx</td></tr> <tr><td>iPhone6 Plus</td> <td>1rpx = 0.552px</td> <td>1px = 1.81rpx</td></tr></tbody></table> <p>并且官网注明了一些注意项</p> <blockquote><p>建议： 开发微信小程序时设计师可以用 iPhone6 作为视觉稿的标准。
注意： 在较小的屏幕上不可避免的会有一些毛刺，请在开发时尽量避免这种情况。</p></blockquote> <p><b>样式导入</b>
使用@import语句可以导入外联样式表，@import后跟需要导入的外联样式表的相对路径，用;表示语句结束。</p> <div class="language- extra-class"><pre class="language-text"><code>/** common.wxss **/
.small-p {
  padding:5px;
}
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>/** app.wxss **/
@import &quot;common.wxss&quot;;
.middle-p {
  padding:15px;
}
</code></pre></div><p><b>内联样式</b>
框架组件上支持使用 style、class 属性来控制组件的样式。</p> <ul><li>style：静态的样式统一写到 class 中。style 接收动态的样式，在运行时会进行解析，请尽量避免将静态的样式写进 style 中，以免影响渲染速度。</li></ul> <div class="language- extra-class"><pre class="language-text"><code>&lt;view style=&quot;color:{{color}};&quot; /&gt;
</code></pre></div><ul><li>class：用于指定样式规则，其属性值是样式规则中类选择器名(样式类名)的集合，样式类名不需要带上.，样式类名之间用空格分隔。</li></ul> <div class="language- extra-class"><pre class="language-text"><code>&lt;view class=&quot;normal_view&quot; /&gt;
</code></pre></div><p>那么还有一点需要注意的是<b>全局样式修改</b></p> <p>定义在 app.wxss 中的样式为全局样式，作用于每一个页面。在 page 的 wxss 文件中定义的样式为局部样式，只作用在对应的页面，并会覆盖 app.wxss 中相同的选择器。</p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-4-5 17:45:19</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/notes/notes/wx-use-note/start_04.html" class="prev">第四章 文件部分注意事项</a></span> <span class="next"><a href="/notes/notes/ts/">TS学习</a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><div id="live2d-widget" class="live2d-widget-container" style="position:fixed;right:65px;bottom:0px;width:135px;height:300px;z-index:99999;opacity:0.8;pointer-events:none;"><canvas id="live2d_canvas" width="135" height="300" class="live2d_canvas" style="position:absolute;left:0px;top:0px;width:135px;height:300px;"></canvas></div></div></div>
    <script src="/notes/assets/js/app.f19b5e39.js" defer></script><script src="/notes/assets/js/2.69392339.js" defer></script><script src="/notes/assets/js/55.f724b26c.js" defer></script><script src="/notes/assets/js/14.e376f3f7.js" defer></script>
  </body>
</html>
